/*
 * 
 */
package JavaDictConsoleUI;
import ConsoleInputLib.ConsoleInput;
import JavaDictController.JDController;
import java.util.*;
/**
 *
 * @author Sambya
 */
public class JavaDictConsoleUI {
    /**
     * Jalankan Console User Interface
     */
    public static void Init(){ 
        String kata;
        
        
        String[] dataStructs = JDController.getAvailableDataStruct();
        System.out.println("Pilihan struktur data internal :");
        
        for (int idx = 0; idx < dataStructs.length; idx++) {
            System.out.println(idx+". "+dataStructs[idx]);
        }
        int DesiredDataType = -1;
        
        while ((DesiredDataType<0) || (DesiredDataType>=dataStructs.length)) {            
            System.out.print(">> ");
            DesiredDataType = ConsoleInput.readInt(-1);
        }
        
        JDController cont = new JDController(0,1,DesiredDataType);
        
        System.out.println("Anda telah memilih tipe data "+cont.ViewInternal());
        

        while (true){
            
            //default kamus
            System.out.println("\n [ "+cont.getLangs()[cont.getLangFr()]+" --> "
                    + cont.getLangs()[cont.getLangTo()]+" ] ");
            System.out.print(">> "); 
             
            kata = ConsoleInput.readString().trim().toLowerCase();
            //case handler
            if(kata.startsWith("#")){
                
                if(kata.equals("#add")){
                    
                    //nambah database
                    System.out.print("kata#1:");
                    String kat1 = ConsoleInput.readString();
                    System.out.print("tipe#1:");
                    String kat2 = ConsoleInput.readString();
                    System.out.print("kata#2:");
                    String kat3 = ConsoleInput.readString();
                    System.out.print("tipe#2:");
                    String kat4 = ConsoleInput.readString();
                    
                    if ((kat1.length()>0)&&(kat2.length()>0)&&(kat3.length()>0)&&(kat4.length()>0)){
                        cont.AddEntry(kat1, kat2, kat3, kat4);
                    } else {
                        System.out.println("ERROR: terdapat String kosong");
                    }
                    
                    
                }else if(kata.equals("#set_trans")) {
                    
                    String[] langs = cont.getLangs();
                    for (int i = 0; i < langs.length; i++) {
                        System.out.println(i+". "+langs[i]);
                        }
                    
                    System.out.print("Bahasa asal   : "); 
                    int langfr = ConsoleInput.readInt(-1);
                    System.out.print("Bahasa tujuan : "); 
                    int langto = ConsoleInput.readInt(-1);
                    
                    if ((langfr>=0&&langfr<langs.length)&&(langto>=0&&langto<langs.length)&&(langfr!=langto))
                        cont.ChangeLang(langfr, langto);
                    else
                        System.out.println("ERROR: Input tidak valid");
                    
                }else if(kata.equals("#view_all")){
                    //menampilkan semua entry
                    System.out.println(cont.ViewAll());
                    
                }else if(kata.equals("#view_internal")){
                    //menampilkan struktur internal yang digunakan
                    System.out.println(cont.ViewInternal());
                    
                }else if(kata.equals("#lang")){
                    //mengenali bahasa
                    System.out.println("Cari kata "); 
                    System.out.print(">>> "); 
                    String kata2 = ConsoleInput.readString();
                    
                    if (kata2.length()>0){
                        String temp = cont.DetectLang(kata2);
                        if (temp.length()>0){
                            System.out.println(kata2+" berasal dari bahasa "+temp); 
                        } else {
                            System.out.println(kata2+" tidak ditemukan di bahasa manapun"); 
                        }
                            
                    } else {
                        System.out.println("ERROR: String kosong"); 
                    }
                    
                }else if(kata.equals("#quit")){
                    
                    System.out.println("Bye."); 
                    Runtime runtime = Runtime.getRuntime();
                    runtime.exit(0);
                    
                }else {
                    
                    System.out.println("ERROR: Perintah yang anda masukan salah"); 
                    System.out.println("Perintah tersedia : ");
                    System.out.println("  #add");
                    System.out.println("  #set_trans ");
                    System.out.println("  #view_all ");
                    System.out.println("  #view_internal ");
                    System.out.println("  #lang ");
                    System.out.println("  #quit ");
                    
                }
            }
            else if (kata.length()>0){
                //translate default
                ArrayList<String> arti = cont.Search(kata);
                if (arti!=null) {
                    System.out.println();
                    System.out.println(cont.getLangs()[cont.getLangFr()]+" : ");
                    System.out.println("  "+kata);  
                    System.out.println();    
                    System.out.println(cont.getLangs()[cont.getLangTo()]+" : ");
                    for (Iterator<String> it = arti.iterator(); it.hasNext();) {
                        String temp = it.next();
                        System.out.println("  "+temp); 
                    } 
                } else {
                    System.out.println("Not found");
                }
            }
        }
    } 
}
